define(['helper/tools','chooseBook','pageReading'], function(tools,chooseBook,pageReading){
    var _this = null;
    
    var init = function(){
        _this = this;        
        _this.regBtn.init();
    };
   	var dataTrans = {
   		setNickName : function(username){
   			tools.ajax('user/set_nick_name',{nick_name:username},
				function(res){
                    if(res.status == 1){
                        _this.view.choosePage();
                    }
                    else{
                        tools.tipZone("tip_wrong","该昵称已存在,请重新设置~")
                    }
                }
            )
   		},
   		getUserInfo: function(){
		    tools.ajax('user/user_info',{},
	    		function(res){
	    			tools.__UTILS.userInfo = res;
	    		}
	    	)
			// 没有nick_name
			// tools.__UTILS.userInfo = {"book":1,"chapter":11,"page_number":6,"user_vocabulary_size":30};
			// 有nick_name,没有读书进度
			// tools.__UTILS.userInfo = {"nick_name":'KMY',"user_vocabulary_size":30,'base_vocabulary':3000};
			// 有nick_name,也有读书进度
			 // tools.__UTILS.userInfo = {"nick_name":'KMY',"book":1,"chapter":11,"page_number":6,"user_vocabulary_size":30,'base_vocabulary':3000};
			console.log(tools.__UTILS.userInfo);
			tools.__UTILS.userActionRecord.current_bookId = tools.__UTILS.userInfo.book;
            tools.__UTILS.userActionRecord.current_chapterId = tools.__UTILS.userInfo.chapter;
            tools.__UTILS.userActionRecord.current_pageNumber = tools.__UTILS.userInfo.page_number;
            tools.__UTILS.userActionRecord.current_vocabularySize = tools.__UTILS.userInfo.user_vocabulary_size;
            tools.__UTILS.userActionRecord.current_bookName = tools.__UTILS.userInfo.book_name;
            tools.__UTILS.userActionRecord.chooseBookId = tools.__UTILS.userInfo.book;
            tools.__UTILS.userActionRecord.DefaultPageNumber = tools.__UTILS.userInfo.page_number;
            tools.__UTILS.userActionRecord.chooseBookName = tools.__UTILS.userInfo.book_name;

	    },
        setBaseVocabulary: function(){
            tools.ajax('user/set_base_vocabulary',
                { base_vocabulary : tools.__UTILS.userInfo.base_vocabulary},
                function(res){
                    if(res.status == 1){
                        _this.view.choosePage();
                    }
                    else{
                        tools.tipZone("tip_wrong","设置词汇量失败~")
                    }
                }
            )
        }
   	};
    var regBtn = {
        
    	submit : function(){
    		// tools.loading.init();
    		var username = $(".username").val();
    		console.log("用户名");
    		if(username.trim() != ""){
				_this.dataTrans.setNickName(username);
			}
            else{
                tools.tipZone("tip_wrong","请输入有效用户名~")
            }
    	},
    
        init : function(){
            // 登陆界面
            $(".login_btn").click(function(){
                event.stopPropagation();
                regBtn.submit();
            });
            $(".submit_vocabulary_btn").click(function(){
                event.stopPropagation();
                tools.__UTILS.userInfo.base_vocabulary = $(".selected_vocabulary").data("base");
                var that = this;
                $(this).prop("disabled", true);
                _this.dataTrans.setBaseVocabulary();
                setTimeout(function () {
                    $(that).prop("disabled", false);
                }, 5000);
            });
            // 选择词汇量界面
            $(".selectVocabulary_ul li").bind("click",function(){
                $(".selected_vocabulary").html($(this).html());
                $(".selected_vocabulary").data("base",$(this).val());
                $(this).parent().hide();
            })
            $(".selectVocabulary_dp_ti").bind("click",function(){
                 $(this).siblings().toggle();
            })
        },
    };
    
    var view = {
    	choosePage :function(){
    		_this.dataTrans.getUserInfo();
    		console.log("cookie为"+tools.__UTILS.userInfo.name+"nick_name为"+tools.__UTILS.userInfo.nick_name)
	    	if(!tools.__UTILS.userInfo.nick_name || tools.__UTILS.userInfo.nick_name == null){
	        	// 没有用户名
	        	tools.router("login");
	        }
            else{
                if(!tools.__UTILS.userInfo.base_vocabulary || tools.__UTILS.userInfo.base_vocabulary == null){
                    // 没有选择基础词汇量
                    tools.router("selectVocabulary");
                }
                else{
                   
            	// 有读书进度，直接进读书页面
            	// book,chapter,page_number
                	if(tools.__UTILS.userInfo.page_number){
                		
                		pageReading.init();
                		pageReading.view.init();
                	}
                	// 没读书进度，进入选书页面
                	else{
                		chooseBook.init();
                        chooseBook.view.init();
                        tools.initiallyIntro("chooseBook");
                	}

                }	
            }        
	    }	
    };
    return{
    	init: init,
    	view: view,
    	regBtn: regBtn,
    	dataTrans: dataTrans    	
    }
})